
from apps.common.base.serializers import *
from apps.interface.node.models import *
from apps.interface.advert.serializers import AdvertBaseSerializer, AdvertModelSerializer
from django.apps import apps

class NodeBaseSerializer(BaseSerializer):

    class Meta:
        model = Node
        fields = "__all__"

class NodeModelSerializer(BaseSerializer):

    componentModel = serializers.SerializerMethodField()
    def get_componentModel(self, obj):
        return {'name': obj.component.name, 'title': obj.component.title} if obj.component else {}

    advertList = serializers.SerializerMethodField()
    def get_advertList(self, obj):
        _adverts = obj.adverts.filter(is_delete=0).order_by('sort').select_related('item')
        if _adverts.count()>0:
            return AdvertModelSerializer(_adverts, many=True).data
        else:
            return []
    
    # itemList字段一定要加，否则前端itemList组件不会刷新数据
    itemList = serializers.SerializerMethodField()
    def get_itemList(self, obj):
        return [{}]

    class Meta:
        model = Node
        fields = "__all__"